#ifndef _CLI_INTERPRETER_H
#define _CLI_INTERPRETER_H

/* From FreeRTOS+CLI V1.0.4 */
/* The size of the global output buffer that is available for use when there
are multiple command interpreters running at once (for example, one on a UART
and one on TCP/IP).  This is done to prevent an output buffer being defined by
each implementation - which would waste RAM.  In this case, there is only one
command interpreter running, and it has its own local output buffer, so the
global buffer is just set to be one byte long as it is not used and should not
take up unnecessary RAM. */
#define CLI_MAX_OUTPUT_SIZE     1

/* parameter assert macro */
#define CLI_ASSERT(x)           //while((x) == 0);

/* The prototype to which callback functions used to process command line
commands must comply.  write_buf is a buffer into which the output from
executing the command can be written, buf_len is the length, in bytes of
the write_buf buffer, and cmd_str is the entire string as input by
the user (from which parameters can be extracted).*/
typedef int (*cli_cmd_handler_t)(char *write_buf, int buf_len, const char *cmd_str);


/* The structure that defines command line commands.  A command line command
should be defined by declaring a const structure of this type. */
typedef struct
{
    const char *const cmd_str;                  /* The command that causes cli_cmd_handler to be executed.  For example "help".  Must be all lower case. */
    const char *const help_str;                 /* String that describes how to use the command.  Should start with the command itself, and end with "\r\n".  For example "help: Returns a list of all the commands\r\n". */
    const cli_cmd_handler_t cli_cmd_handler;    /* A pointer to the callback function that will return the output generated by the command. */
    signed char  expect_param_num;              /* Commands expect a fixed number of parameters, which may be zero, if unfixed, set to -1 */
} cli_cmd_t;


/*
 * Register the command passed in using the cli_cmd parameter.
 * Registering a command adds the command to the list of commands that are
 * handled by the command interpreter.  Once a command has been registered it
 * can be executed from the command line.
 */
int cli_cmd_register(const cli_cmd_t *const cli_cmd);


/*
 * Runs the command interpreter for the command string "cmd_input".  Any
 * output generated by running the command will be placed into write_buf.
 * buf_len must indicate the size, in bytes, of the buffer pointed to
 * by write_buf.
 *
 * cli_cmd_process should be called repeatedly until it returns 0.
 *
 * pcCmdIntProcessCommand is not reentrant.  It must not be called from more
 * than one task - or at least - by more than one task at a time.
 */
int cli_cmd_process(const char *const cmd_input, char *write_buf, int buf_len);


/*-----------------------------------------------------------*/

/*
 * A buffer into which command outputs can be written is declared in the
 * main command interpreter, rather than in the command console implementation,
 * to allow application that provide access to the command console via multiple
 * interfaces to share a buffer, and therefore save RAM.  Note, however, that
 * the command interpreter itself is not re-entrant, so only one command
 * console interface can be used at any one time.  For that reason, no attempt
 * is made to provide any mutual exclusion mechanism on the output buffer.
 *
 * cli_get_output_buf() returns the address of the output buffer.
 */
char *cli_get_output_buf(void);


/*
 * Return a pointer to the wanted_param'th word in cmd_str. param_str_len can be NULL
 */
const char *cli_get_param(const char *cmd_str, int wanted_param, int *param_str_len);

/*
 * Insert '\0' after every param, befor call it, make sure you have get all params
 */
int cli_get_param_end(const char *cmd_str);


#endif /* _CLI_INTERPRETER_H */
